package com.woniuxy.service;

import java.util.List;

import com.woniuxy.dao.UserDAO;
import com.woniuxy.entity.User;
import com.woniuxy.util.PageUtil;

public class UserService {
	
	private UserDAO dao;
	
	public UserService() {
		this.dao = new UserDAO();
	}
	
	public List<User> showAll(){
		if(PageUtil.getUsers() == null){
			PageUtil.setUsers(dao.getAllUser());
			PageUtil.getIntance().setPage(1);
		}
		System.out.println("page:"+PageUtil.getIntance().getPage());
		return PageUtil.getIntance().getShow(); 
	}
	
	public User findUser(String userName){
		return dao.getUserByName(userName);
	}
	
	public boolean addUser(User user){
		if(isRegister(user.getUserName()))
			return dao.insertUser(user);
		return false;
	}
	
	public boolean deleteUser(User user){
		if(user.getId() == null)
			return false;
		return dao.delete(user);
	}
	
	public boolean isLogin(String userName,String passWord){
		User user = dao.getUserByName(userName);
		if(user != null){
			if(userName.equals(user.getUserName())&&passWord.equals(user.getPassWord())){
				return true;
			}
		}
		return false;
	}
	
	public boolean isRegister(String userName){
		User user = dao.getUserByName(userName);
		if(user == null){
			return true;
		}
		return false;
	}
}
